package com.example.otto.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.otto.entity.Task;
import org.apache.ibatis.annotations.*;

import java.sql.Date;
import java.util.List;

@Mapper
public interface TaskMapper extends BaseMapper<Task> {

    @Select("select * from task")
    List<Task> findAll();

    @Insert("insert into task(date,position,description,name,phone,userId,workerId,workerPhone,state)" +
            "values(#{date},#{position},#{description},#{name},#{phone},#{userId},#{workerId},#{workerPhone},#{state})")
    Boolean add(Task task);

    Integer update(Task task);

    @Select("select * from task limit #{pageNum},#{pageSize}")
    List<Task> selectPage(Integer pageNum, Integer pageSize);

    @Select("select count(*) from task")
    Integer selectTotal();

    @Select("select * from task where name = #{name}")
    List<Task> findByName(@Param("name") String task);

    @Select("select * from task where DATE(date) = #{date, jdbcType=DATE}")
    List<Task> findByDate(Date date);

    List<Task> findByDate_Name(String name, Date date);

    @Select("select phone from worker where id = #{workerId}")
    String findPhoneByWorkerId(String workerId);

    @Select("select * from task where workerId = #{id} and state != 2 limit 1")
    Task workerView(Integer id);

    @Select("select id from worker where state = 0 limit 1")
    String findDefaultWorker();

    @Update("update task set state = 2 where num = #{num}")
    Boolean confirm(Integer num);

//    @Select("select * from task where workerId = #{id} and state = 2")
//    List<Task> findFinishedTaskByWorkerId(Integer id);

    @Select("select * from task where workerId = {id} and state = 1 limit 1")
    Task findHandlingTaskByWorkerId(Integer id);

    @Select("select workerId from task where num = #{num}")
    String  findHandlingWorkerId(Integer num);
}
